<template>
  <el-row>
    <el-col :span="6">
      <div class="box">
        <el-scrollbar>
          <basic-container>
            <el-input v-model="filterText" placeholder="输入关键字过滤"/>
            <el-tree
              ref="tree"
              class="filter-tree"
              :data="treedata"
              :props="defaultProps"
              default-expand-all
              highlight-current
              node-key="id"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              @node-click="nodeClick"></el-tree>
          </basic-container>
        </el-scrollbar>
      </div>
    </el-col>

    <el-col :span="18">
      <basic-container>
        <avue-crud :option="option"
                   :table-loading="loading"
                   :data="data"
                   :page="page"
                   :permission="permissionList"
                   :before-open="beforeOpen"
                   v-model="form"
                   ref="crud"
                   @row-update="rowUpdate"
                   @row-save="rowSave"
                   @row-del="rowDel"
                   @search-change="searchChange"
                   @search-reset="searchReset"
                   @selection-change="selectionChange"
                   @current-change="currentChange"
                   @refresh-change="refreshChange"
                   @size-change="sizeChange"
                   @on-load="onLoad">
          <template slot="menuLeft">
            <el-button type="danger"
                       size="small"
                       icon="el-icon-delete"
                       plain
                       v-if="permission.rtcphone_delete"
                       @click="handleDelete">删 除
            </el-button>
          </template>
        </avue-crud>
      </basic-container>
    </el-col>
  </el-row>
</template>

<script>
  import {getPage, getList, getDetail, add, update, remove} from "@/api/bureau/phone/rtcphone";
  import {getTree} from "@/api/bureau/miniaccident/rtcseat";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        filterText: '',
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        defaultProps: {
          children: 'children',
          label: 'seatName'
        },
        treedata: [],
        selectDep: null,
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: false,
          viewBtn: true,
          selection: true,
          column: [
            // {
            //   label: "坐席编号",
            //   prop: "seatId",
            //   rules: [{
            //     required: true,
            //     message: "请输入坐席编号",
            //     trigger: "blur"
            //   }]
            // },

            {
              label: "手机品牌",
              prop: "brand",
              rules: [{
                required: true,
                message: "请输入手机品牌",
                trigger: "blur"
              }]
            },
            {
              label: "IMEI编码",
              prop: "imei",
              rules: [{
                required: false,
                message: "请输入手机IMEI编码",
                trigger: "blur"
              }]
            },
            {
              label: "资产编号",
              prop: "assetsNumber",
              rules: [{
                required: false,
                message: "请输入手机固定资产编号",
                trigger: "blur"
              }]
            },
            {
              label: "微信openid",
              prop: "openid",
              rules: [{
                required: false,
                message: "请输入安装的微信用户的openid",
                trigger: "blur"
              }]
            },
            {
              label: "坐席编号",
              prop: "seatId",
              type: "tree",
              defaultExpandAll: true,
              dicData: [],
              slot: true,
              props: {
                label: "seatName",
                value: "id",
                children: "children"
              },
              rules: [{
                required: true,
                message: "请输入坐席号",
                trigger: "blur"
              }]
            },
            {
              label: "坐席名称",
              prop: "seatName",
              addDisplay: false,
              editDisplay: false,
              rules: [{
                required: false,
                message: "请输入坐席名称",
                trigger: "blur"
              }]
            },
            {
              label: "是否启用",
              prop: "status",
              type: "radio",
              value: 1,
              dicData: [
                {
                  label: "否",
                  value: 0
                },
                {
                  label: "是",
                  value: 1
                }
              ],
              rules: [{
                required: true,
                message: "请输入是否启用",
                trigger: "blur"
              }]
            },

          ]
        },
        data: []
      };
    },
    watch: {
      'form.tenantId'() {
        if (this.form.tenantId !== '') {
          getTree(this.form.tenantId).then(res => {
            const column = this.findObject(this.option.column, "seatId");
            column.dicData = res.data.data;
          });
        }
      },
      filterText(val) {
        this.$refs.tree.filter(val)
      },
      selectDep(val) {
        this.query.seatId = val;
        getPage(this.page.currentPage, this.page.pageSize, {seatId: val}).then(res => {
          const data = res.data;
          this.page.total = data.count;
          this.data = data.data;
          this.loading = false;
        });
      }
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.rtcphone_add, false),
          viewBtn: this.vaildData(this.permission.rtcphone_view, false),
          delBtn: this.vaildData(this.permission.rtcphone_delete, false),
          editBtn: this.vaildData(this.permission.rtcphone_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        // this.onLoad(this.page, this.query);
        getPage(this.page.currentPage, this.page.pageSize, {seatId: this.query.seatId}).then(res => {
          const data = res.data;
          this.page.total = data.count;
          this.data = data.data;
          this.loading = false;
        });
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getTree().then(resTree => {
          let _resTree = resTree.data.data;
          const column = this.findObject(this.option.column, "seatId");
          column.dicData = _resTree;
          this.treedata = _resTree;
          console.log(_resTree[0])
          this.query.seatId = this.treedata[0].id;
          if (_resTree.length > 0) {
            getPage(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
              const data = res.data;
              this.page.total = data.count;
              this.data = data.data;
              this.selectionClear();
              this.loading = false;
            });
          }
        })
      },
      filterNode(value, data) {
        if (!value) return true
        return data.seatName.indexOf(value) !== -1
      },
      nodeClick(data, node, item) {
        this.selectDep = data.id;
      },
    }
  };

</script>

<style>
</style>
